<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link href="css/header.css" rel="stylesheet" />
	</head>

	<body>
		
		<!-- 标题栏 -->
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white;"></a>
			<!-- 制作...按键 -->
			<a id="submit_nickname" class="mui-icon mui-icon-right-nav mui-pull-right" style="color: white;font-size: 14px;line-height: 26px;">完成</a>
			<h1 class="mui-title title-color">我的头像</h1>
		</header>
		
		<div class="mui-content">
			<div class="mui-input-row">
				<input id="my_nickname" type="text" class="mui-input-speech mui-input-clear" placeholder="请输入昵称">
			</div>
			
		</div>
				
		<script src="js/mui.js"></script>
		<script src="js/app.js" ></script>
		<script type="text/javascript">
			mui.init();
			
			mui.plusReady(function () {
			    
				//显示当前用户的昵称
				var user =  app.getUserGlobalInfo();
				var nickname = user.nickname;
				//显示在当前页面上
				var my_nickname = document.getElementById("my_nickname");
				my_nickname.value = nickname;
				
				//完成按钮添加确定点击事件
				var submit_nickname = document.getElementById("submit_nickname");
				submit_nickname.addEventListener("tap",function(){
					var newNickname = my_nickname.value;
					//做前端判断
					if(newNickname.length > 8 ){
						app.showToast("昵称长度不能超过8……");
						return false;
					}
					if(newNickname.length < 1){
						app.showToast("修改昵称不能为空");
						return false;
					}
					
					//1. 显示等待框
					plus.nativeUI.showWaiting("请稍候……");
					
					//2. 发送ajax请求, 参数为用户id和用户昵称
					mui.ajax(app.serverUrl + "/user/setNickname",{
						data:{
							userId: user.id,
							nickname: newNickname
						},
						dataType:'json',//服务器返回json格式数据
						type:'post',//HTTP请求类型
						timeout:10000,//超时时间设置为10秒；
						headers:{'Content-Type':'application/json'},	              
						success:function(data){
							
							// 关闭等待框
							plus.nativeUI.closeWaiting();
							
							if (data.status == 200) {
								//上传成功,前台返回user对象
								var userInfo = data.data;
								console.log("更新用户昵称成功");
								//更新页面的user
								app.setUserGlobalInfo(userInfo);
								
								// 触发另外一个webview的自定义事件，可以使用 mui.fire()
								//1.获取webview对象
								var webview_me =  plus.webview.getWebviewById("easychat-me.html");
								//2. 触发事件
								mui.fire(webview_me,"refresh");
								
								// 页面跳转回,我的页面
								mui.back();
								//mui.openWindow("easychat-me.html", "easychat.html");
							} else {
								plus.nativeUI.closeWaiting();
								app.showToast(data.msg, "error");
							}
						}
					});
					
				})
				
			});
			
		</script>
	</body>

</html>
